using System;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Notifications;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
using DBCHM.Service;
using DBCHM.Utils.Events;
using DBCHM.ViewModels;
using DBCHM.Views;
using Microsoft.Extensions.DependencyInjection;

namespace DBCHM;

public partial class App : Application
{
    public App()
    {
        Services = ConfigureServices();
    }

    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);
    }

    public override void OnFrameworkInitializationCompleted()
    {
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            // Line below is needed to remove Avalonia data validation.
            // Without this line you will get duplicate validations from both Avalonia and CT
            BindingPlugins.DataValidators.RemoveAt(0);
            desktop.MainWindow = Services.GetRequiredService<MainWindow>();
        }

        base.OnFrameworkInitializationCompleted();
    }

    /// <summary>
    /// Gets the current <see cref="App"/> instance in use
    /// </summary>
    public static new App Current => (App)Avalonia.Application.Current!;

    /// <summary>
    /// 通知页面
    /// </summary>
    public static WindowNotificationManager NotificationPage { get; set; }

    /// <summary>
    /// Gets the <see cref="IServiceProvider"/> instance to resolve application services.
    /// </summary>
    public IServiceProvider Services { get; }

    /// <summary>
    /// Configures the services for the application.
    /// </summary>
    private static IServiceProvider ConfigureServices()
    {
        var services = new ServiceCollection();
        services.AddTransient<IMessageService, MessageService>();
        services.AddTransient<IStoreService, SqliteService>();
        services.AddTransient<IBusinessService, BusinessService>();

        services.AddTransient<MainWindow>();
        services.AddTransient<MainWindowViewModel>();
        services.AddTransient<IndexPageView>();
        services.AddTransient<IndexPageViewModel>();
        services.AddTransient<AddConnectWindowView>();
        services.AddTransient<AddConnectWindowViewModel>();
        services.AddTransient<ConnectListPageView>();
        services.AddTransient<ConnectListPageViewModel>();


        return services.BuildServiceProvider();
    }
}